package com.ofood.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.ofood.springcloud.domain.Result;
import com.ofood.springcloud.service.IFoodFeignService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@DefaultProperties(defaultFallback = "defaultFallbackTimeout",commandProperties = {
        @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "13000")
})
@RestController
public class FoodFeignController {
    @Resource
    private IFoodFeignService foodFeignService;

    @GetMapping("/feign/hystrix/consumer/food/get/{foodId}")
    public Result getFoodById(@PathVariable("foodId") long foodId) {
        return foodFeignService.getFoodById(foodId);
    }


//    @HystrixCommand(fallbackMethod = "fallbackTimeout", commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
//    })
    @HystrixCommand
    @GetMapping("/feign/hystrix/consumer/food/get/timeout")
    public String getFoodTimeout() {
        return foodFeignService.getFoodTimeoutHystrix();
    }

    @HystrixCommand(fallbackMethod = "fallbackError", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    })
    @GetMapping("/feign/hystrix/consumer/food/get/error")
    public String getFoodError() {
        return foodFeignService.getFoodErrorHystrix();
    }

    public String fallbackTimeout(){
        return "哈哈哈超时了consumer！";
    }

    public String fallbackError(){
        return "哈哈哈报异常了consumer！";
    }

    public String defaultFallbackTimeout(){
        return "哈哈哈，默认的服务降级！";
    }
}
